// pages/createPatient/index.js
const api = require("../../../config/api")
const request = require("../../../utils/request")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isEdit: 0,
		patientInfo: {
			age: '',
			birthday: "",
			idCard: "",
			nation: "",
			patientName: "",
			isComplete: 1
		},
		today: '',
		showModalStatus: false,

		twoWord: [
			'汉族', '回族', '满族', '土族', '藏族', '羌族', '彝族', '傣族',
			'佤族', '怒族', '苗族', '侗族', '水族', '壮族', '瑶族', '京族',
			'黎族', '畲族', '白族'
		],
		threeWord: [
			'蒙古族', '朝鲜族', '赫哲族', '东乡族', '撒拉族', '保安族',
			'裕固族', '门巴族', '珞巴族', '哈尼族', '僳僳族', '拉祜族',
			'纳西族', '景颇族', '布朗族', '阿昌族', '普米族', '德昂族',
			'独龙族', '基诺族', '布依族', '仡佬族', '仫佬族', '毛南族',
			'土家族', '高山族', '锡伯族'
		],
		fourWord: [
			'鄂温克族', '达斡尔族', '鄂伦春族', '塔吉克族', '俄罗斯族',
			'维吾尔族', '哈萨克族', '塔塔尔族'
		],
		fiveWord: [
			'柯尔克孜族', '乌孜别克族'
		]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var timestamp = Date.parse(new Date());
		var date = new Date(timestamp);
		//获取年份  
		var Y = date.getFullYear();
		//获取月份  
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
		//获取当日日期 
		var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
		this.setData({
			today: Y + '-' + M + '-' + D
		})

		if (options.patientInfo) {
			const patientInfo = JSON.parse(options.patientInfo)
			patientInfo.birthday = patientInfo.birthday.substring(0, 10)
			this.setData({
				patientInfo: patientInfo,
				isEdit: 1
			})
		}
	},

	bindDateChange(e) {
		let patientInfo = this.data.patientInfo
		patientInfo.birthday = e.detail.value
		this.setData({
			patientInfo: patientInfo
		})
	},
	sexChange(e) {
		let patientInfo = this.data.patientInfo
		patientInfo.sex = Number(e.detail.value)
		this.setData({
			patientInfo: patientInfo
		})
	},
	idCart(e) {
		let patientInfo = this.data.patientInfo
		const idCard = e.detail.value
		const idCardNo = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		if (!idCardNo.test(idCard)) {
			return
		}
		// 截取出生日期
		let birthdayno, birthdaytemp, sexno, sex
		if (idCard.length === 18) {
			birthdayno = idCard.substring(6, 14)
			sexno = idCard.substring(16, 17)
		} else if (idCard.length === 15) {
			birthdaytemp = idCard.substring(6, 12)
			sexno = idCard.substring(14, 15)
			birthdayno = "19" + birthdaytemp
		}
		let birthday = birthdayno.substring(0, 4) + "-" + birthdayno.substring(4, 6) + "-" + birthdayno
			.substring(6, 8)
		// 获取性别
		var tempid = sexno % 2;
		if (tempid == 0) {
			sex = 2
		} else {
			sex = 1
		}
		patientInfo.idCard = idCard
		patientInfo.birthday = birthday
		patientInfo.sex = sex
		this.setData({
			patientInfo: patientInfo
		})
	},
	nation() {
		this.setData({
			showModalStatus: true
		})
	},
	chooseNation(val) {
		let patientInfo = this.data.patientInfo
		patientInfo.nation = val.currentTarget.dataset.item
		this.setData({
			patientInfo: patientInfo
		})
	},
	patientName(val) {
		let patientInfo = this.data.patientInfo
		patientInfo.patientName = val.detail.value
		this.setData({
			patientInfo: patientInfo
		})
	},
	savaBtnTap() {
		this.setData({
			showModalStatus: false
		})
	},
	next: async function() {
		let patientInfo = this.data.patientInfo
		if (!patientInfo.patientName) {
			wx.showToast({
				title: '请填写患者姓名',
				icon: "none",
			})
			return
		}
		if (!patientInfo.idCard) {
			wx.showToast({
				title: '请填写患者身份证号',
				icon: "none",
			})
			return
		}
		if (!patientInfo.nation) {
			wx.showToast({
				title: '请选择民族',
				icon: "none",
			})
			return
		}
		const today = new Date();
		const thisYear = today.getFullYear()
		const choseday = patientInfo.birthday.split('-')
		patientInfo.age = thisYear - choseday[0]
		patientInfo.birthday = patientInfo.birthday + ' 00:00:00'
		const userId = wx.getStorageSync('userId')
		patientInfo.appletUserId = userId
		const res = await request.get(api.selectByIdCard + '/' + patientInfo.idCard)
		if (res.httpStatus === 200) {
			wx.showModal({
				content: '该患者已经注册，是否前往绑定患者？',
				cancelText: "取消", //默认是“取消”
				confirmText: "去绑定", //默认是“确定”
				success: function(data) {
					if (data.cancel) {
						//点击取消,默认隐藏弹框
					} else {
						//点击确定
						const patientDate = JSON.stringify(res.data)
						wx.navigateTo({
							url: '../../../bindPatient/index?patientDate=' +
								patientDate
						})
					}
				},
				fail: function(data) {}, //接口调用失败的回调函数 
				complete: function(data) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
			})
		} else {
			const res = await request.post(api.savePatient, patientInfo)
			if (res.httpStatus === 200) {
				// this.bindPatient()
				const patientInfo = {
					id: res.data
				}
				wx.showToast({
					title: '绑定成功',
				})
				const urlParmas = JSON.stringify(patientInfo)
				setTimeout(function() {
					wx.navigateTo({
						url: './highRiskInfo?patient=' + urlParmas
					})
				}, 1000)
			}
		}

	},
	bindPatient: async function() {
		const userId = wx.getStorageSync('userId')
		const patientId = this.data.patientInfo.id
		const data = {
			userId: userId,
			patientId: patientId

		}
		const res = await request.post(api.bindPatient, data)
		if (res.httpStatus === 200) {
			wx.showToast({
				title: '绑定成功',
			})
			setTimeout(function() {
				wx.navigateTo({
					url: './highRiskInfo'
				})
			}, 1000)
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
